package org.tech4j.springboot.starter.captcha.graphical;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tech4j.captcha.graphical.config.CaptchaGraphicalConfig;
import org.tech4j.captcha.graphical.service.CaptchaService;
import org.tech4j.common.spi.ExtensionLoader;

/**
 * @program: tech4j
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/5 15:51
 **/
@Configuration
public class CaptchaGraphicalConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "tech4j.captcha.graphical")
    public CaptchaGraphicalConfig captchaGraphicalConfig() {
        return new CaptchaGraphicalConfig();
    }

    @Bean
    public CaptchaService captchaService(final CaptchaGraphicalConfig captchaGraphicalConfig) {
        CaptchaService captchaService = ExtensionLoader.getExtensionLoader(CaptchaService.class).getJoin(captchaGraphicalConfig.getServiceName());
        captchaService.init(captchaGraphicalConfig);
        CaptchaServiceFactory.setCaptchaService(captchaService);
        return captchaService;
    }
}
